package com.hopu.blb_erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.blb_erp.entity.TPrinter;
import com.hopu.blb_erp.service.TPrinterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@Api
@RestController
public class TPrinterController {

    @Autowired
    private TPrinterService tPrinterService;

    @ApiOperation("分页模糊查询打印机")
    @GetMapping("/TPrinterLike/like")
    public ResponseEntity<Page>findLikePage(Integer current, Integer limit,String pNum){
        if("0".equals(pNum)){
            pNum=null;
        }
        QueryWrapper<TPrinter> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(pNum),"p_num",pNum);
        Page<TPrinter> printerPage = new Page<>(current, limit);
        Page<TPrinter> page = tPrinterService.page(printerPage,queryWrapper);
        return ResponseEntity.ok(page);
    }

    @ApiOperation("添加打印机")
    @PostMapping("/TPrinter")
    public ResponseEntity<String>addTPrinter(@RequestBody TPrinter tPrinter){
        tPrinterService.save(tPrinter);
        return ResponseEntity.ok("ok");
    }
    @ApiOperation("更打印机数据")
    @PutMapping("/TPrinter")
    public ResponseEntity<String> modifyTPrinter(@RequestBody TPrinter tPrinter){
        tPrinterService.updateById(tPrinter);
        return ResponseEntity.ok("ok");
    }


//
    @ApiOperation("删除打印机")
    @DeleteMapping("/TPrinter/{id}")
    public ResponseEntity<String>removeTPrinter(@PathVariable("id") Integer id){
        tPrinterService.removeById(id);
        return ResponseEntity.ok("ok");
    }
    @ApiOperation("批量删除打印机")
    @DeleteMapping("/TPrinters/{ids}")
    public ResponseEntity<String>removeTPrinters(@PathVariable("ids") String[] ids){
        tPrinterService.removeByIds(Arrays.asList(ids));
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("根据id查询打印机")
    @GetMapping("/TPrinter/{id}")
    public ResponseEntity<TPrinter> findIdTPrinter(@PathVariable("id") Integer id){
        TPrinter byId = tPrinterService.getById(id);
        return ResponseEntity.ok(byId);
    }

    @ApiOperation("查询所有打印机")
    @GetMapping("/TPrinters")
    public ResponseEntity<List<TPrinter>>findAllTPrinters(){
        List<TPrinter> list = tPrinterService.list();
        return ResponseEntity.ok(list);
    }
}
